import {
  findComponentDownward,
  merge,
  getRoute,
  findComponentUpward,
  getUrlParam,
  windowClose
} from '@admin/utils/index'
import pageBanner from '../../page-banner/index'
import pageChart from '../../page-chart/index'
import Vue from 'vue'
import TablePlugin from '@admin/plugins/table'
import SlotRender from '../components/SlotRender'
export default {
  props: {
    pageInfo: Object,
    code: {
      type: String,
      default: ''
    },
    id: {
      type: String,
      default: ''
    }
  },
  components: {
    pageBanner,
    SlotRender,
    pageChart
  },
  data() {
    return {
      tableInstance: null,
      treeInstance: null,
      commonInstance: null
    }
  },
  beforeMount() {
    this.commonInstance = findComponentUpward(this, 'CommonPage')
  },
  computed: {
    tmpCode: function() {
      return this.pageInfo.tmp
    },
    btns: function() {
      return this.pageInfo.btns.map(btn => {
        btn.disType = btn.disType || '10'
        btn.refButton = (btn.refButton || '').split(',')
        const defaultSetting = {
          confirm: false,
          icon: {
            'icon-class': '',
            type: 'icon'
          }
        }
        btn.renderSetting = merge(defaultSetting, (JSON.parse(btn.renderSetting) || {}))
        return btn
      })
    },
    toolbarBtns: function() {
      return this.btns.filter((btn) => btn.disType === '10')
    },
    tableBtns: function() {
      return this.btns.filter((btn) => btn.disType === '20')
    },
    tableLinkBtns: function () {
      return this.btns.filter((btn) => btn.disType === '30')
    },
    treeBtns: function() {
      return this.btns.filter((btn) => btn.disType === '40')
    },
    // 自定义显示位置
    customBtns: function() {
      return this.btns.filter((btn) => btn.disType === '50')
    },
    // 子表按钮工具栏/表格中
    custFormBtns: function() {
      return this.btns.filter((btn) => ['100', '110'].includes(btn.disType))
    },
    gridInfo: function() {
      return this.pageInfo.gridInfo ? JSON.parse(this.pageInfo.gridInfo) : {}
    },
    search: function() {
      return this.pageInfo.search
    },
    treeInfo: function() {
      return this.pageInfo.treeInfo ? JSON.parse(this.pageInfo.treeInfo) : {}
    },
    tabInfo: function() {
      return this.pageInfo.treeInfo ? JSON.parse(this.pageInfo.treeInfo) : {}
    },
    formSetting: function() {
      try {
        return this.pageInfo.formSetting ? JSON.parse(this.pageInfo.formSetting) : {}
      } catch (error) {
        console.error(error)
        return {}
      }
    },
    // 父页面code，用于返回
    pCode: function() {
      return this.$route.query?._pcode
    },
    showBack: function() {
      return !!this.pCode
    }
  },
  mounted() {
    TablePlugin._setTableInstance(this)
  },
  methods: {
    // 创建vue组件
    createEle() {
      const that = this
      return Vue.extend({
        data() {
          return {
            formInstance: that
          }
        }
      })
    },
    // 树点击
    emitNodeclick(emitData) {
      this.commonInstance && this.commonInstance.$emit('nodeClick', emitData)
      this.$bus.$emit('node-click', emitData)
    },
    // tab点击
    emitTabclick(emitData) {
      this.commonInstance && this.commonInstance.$emit('tabClick', emitData)
      this.$bus.$emit('tab-click', emitData)
    },
    getFormInfo() {
      return this.form
    },
    getTableInstance() {
      return this.tableInstance || (this.tableInstance = findComponentDownward(this, 'LbTable'))
    },
    getTreeInstance() {
      return this.treeInstance || (this.treeInstance = findComponentDownward(this, 'CustomTree'))
    },
    getCurrentRow() {
      this.getTableInstance()
      return new Promise((resolve, reject) => {
        if (!this.tableInstance) {
          reject('请选择行')
        } else {
          this.tableInstance.getCurrentRow().then(data => {
            resolve(data)
          }).catch((msg) => {
            reject(msg)
          })
        }
      })
    },
    getSelectionRows() {
      this.getTableInstance()
      return new Promise((resolve, reject) => {
        if (!this.tableInstance) {
          reject('请选择行')
        } else {
          this.tableInstance.getSelectionRows().then(data => {
            resolve(data)
          }).catch((msg) => {
            reject(msg)
          })
        }
      })
    },
    getTreeNode() {
      return new Promise((resolve, reject) => {
        const treeNode = this.currentTree
        if (Object.keys(treeNode).length) {
          resolve(treeNode)
        } else {
          reject('请选择树节点')
        }
      })
    },
    // 获取表单数据
    getFormData() {
      if (this.tmpCode === '1140') {
        return this.$refs.complexForm.getFullFormData()
      }
      return {}
    },
    // 页面返回
    goBack() {
      if (getUrlParam('_layout') === '0,0') {
        windowClose()
        return
      }
      const view = this.$route
      this.$store.dispatch('tagsView/delView', view).then(({
        visitedViews
      }) => {
        const route = getRoute(this.pCode)
        this.$router.push({
          path: route.path
        })
      })
    }
  }
}
